package com.ruoyi.web.controller.system.api.cc;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.interceptor.Authentication;
import com.ruoyi.system.api.ResponseWithData;
import com.ruoyi.system.api.domain.Login;
import com.ruoyi.system.api.domain.User;
import com.ruoyi.system.api.service.CcService;
import com.ruoyi.system.api.service.IUserService;
import com.ruoyi.system.api.service.LoginService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * @ClassName UserController
 * @Description: 用户信息
 * @Author shihx
 * @Date 2020/2/24
 * @Version V1.0
 **/
@Api(tags="用户信息")
@Controller
@RequestMapping("/api/cc/user")
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;

    @Autowired
    private CcService ccService;


    private static final Logger log = LoggerFactory.getLogger( UserController.class);


    /**
     * 获取FSE基本信息接口
     * @methodName: getFseInformation
     * @description: TODO
     * @param EnginnerId
     * @return: com.ruoyi.system.api.ResponseWithData<?>
     * @author: shihx
     * @date: 2020/3/6 8:46
    **/
    @GetMapping("/getFseInformation")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "获取FSE基本信息接口", httpMethod = "GET", notes = "获取FSE基本信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "服务单主键ID", name = "ServiceNoticeId", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "员工ID", name = "EnginnerId", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> getFseInformation(
            @RequestParam(value="ServiceNoticeId",required=true) String ServiceNoticeId,
            @RequestParam(value="EnginnerId",required=true) String EnginnerId
    ){
        try {
            return userService.getFseInformation(ServiceNoticeId, EnginnerId);
        }catch (Exception e){
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 获取FSE本地用户基本信息接口
     * @methodName: getFseInformation
     * @description: TODO
     * @param EnginnerId
     * @return: com.ruoyi.system.api.ResponseWithData<?>
     * @author: wlf
     * @date: 2020/4/20 16:41
     **/
    @GetMapping("/getStUserMessage")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "获取FSE本地用户基本信息接口", httpMethod = "GET", notes = "获取FSE本地用户基本信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "员工ID", name = "EnginnerId", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> getStUserMessage(
            @RequestParam(value="EnginnerId",required=true) String EnginnerId
    ){
        try {
            return userService.getStUserMessage(EnginnerId);
        }catch (Exception e){
            e.printStackTrace();
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 根据员工ID更新推送标识
     * @methodName: updateRegistrationId
     * @description: TODO
     * @param registrationId
     * @param EnginnerId
     * @return: com.ruoyi.system.api.ResponseWithData<?>
     * @author: shihx
     * @date: 2020-04-01 13:20
    **/
    @Log(title = "根据员工ID更新推送标识", businessType = BusinessType.UPDATE)
    @GetMapping("/updateRegistrationId")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "根据员工ID更新推送标识", httpMethod = "GET", notes = "根据员工ID更新推送标识")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "手机推送标识", name = "registrationId", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "员工ID", name = "EnginnerId", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> updateRegistrationId(
            @RequestParam(value="registrationId",required=true) String registrationId,
            @RequestParam(value="EnginnerId",required=true) String EnginnerId
    ){
        try {
            return userService.updateRegistrationId(registrationId, EnginnerId);
        }catch (Exception e){
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 验证当前用户是否休假
     * @methodName: checkUserWorkDay
     * @description: TODO
     * @param enginnerId
     * @return: com.ruoyi.system.api.ResponseWithData<?>
     * @author: shihx
     * @date: 2020-04-08 14:12
    **/
    @GetMapping("/checkUserWorkDay")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "验证当前用户是否休假", httpMethod = "GET", notes = "验证当前用户是否休假")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "员工ID", name = "EnginnerId", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> checkUserWorkDay(
            @RequestParam(value="enginnerId",required=true) String enginnerId
    ){
        try {
            return userService.checkUserWorkDay(enginnerId);
        }catch (Exception e){
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 用户休假接口
     * @methodName: updateWorkday
     * @description: TODO
     * @param enginnerId
     * @param workday
     * @return: com.ruoyi.system.api.ResponseWithData<?>
     * @author: shihx
     * @date: 2020-04-09 10:02
    **/
    @Log(title = "用户休假接口", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/updateUserWorkday")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "用户休假接口", httpMethod = "GET", notes = "用户休假接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "工程师ID", name = "enginnerId", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "休假状态（1上班、3休假）", name = "workday", dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> updateUserWorkday(
            @RequestParam(value="enginnerId",required=true) String enginnerId,
            @RequestParam(value="workday",required=true) String workday
    ){
        try {
            if(StringUtils.isEmpty(enginnerId)){
                return new ResponseWithData().error().msg("工程师ID(enginnerId)不可以为空").code(201);
            }
            if(StringUtils.isEmpty(workday)){
                return new ResponseWithData().error().msg("休假状态(workday)不可以为空").code(202);
            }
//            String state = "";
//            if("0".equals(workday)){
//                state = "1";
//            }
//            if("1".equals(workday)){
//                state = "3";
//            }
            JSONObject jsonObject = ccService.updateFseStateInfo(enginnerId, workday);
            if("200".equals(jsonObject.getString("code"))){
                User user = new User();
                user.setEnginnerId(Long.parseLong(enginnerId));
                user.setWorkday(workday);
                int i = userService.updateUserByEnginnerId(user);
                return new ResponseWithData().success(jsonObject).msg("操作成功").code(200);
            }else{
                return new ResponseWithData().error().msg("操作失败").code(203);
            }

        }catch (Exception e){
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }



}
